package com.asiainfo.kbisheet.components.condition.ddlist
{
	import com.asiainfo.kbisheet.common.Util;
	import com.asiainfo.kbisheet.components.condition.ConditionItem;
	import com.asiainfo.kbisheet.event.GetListDimResultEvent;
	import com.asiainfo.kbisheet.model.SettingModel;
	import com.asiainfo.kbisheet.vo.DimVO;
	import com.asiainfo.kbisheet.vo.LabelValueVO;
	import com.asiainfo.kbisheet.vo.WhereVO;
	
	import mx.collections.ArrayCollection;
	
	/**
	 * 下拉框的ConditionItem实现。
	 */
	public class ConditionItemDDList extends ConditionItem
	{
		public function ConditionItemDDList(xml:XML)
		{
			super(xml);
		}
		
		override public function initData():void{
			updateMessage("ConditionItem.initDataListDim():加载数据...");
			/*创建IKbsControl的实例*/
			if(kbsControl == null){
				kbsControl = SettingModel.getInstance().getKbsControl();
				kbsControl.addEventListener("getListDimResult",getListDimResult);
			}
			if(conditionXML.hasOwnProperty('dim')){
				var dimXML:XML = XML(conditionXML.dim);
				var wheres:ArrayCollection = new ArrayCollection();
				if(conditionXML.hasOwnProperty('refresh')){
					/*条件*/
					for each(var refreshXML:XML in conditionXML.refresh){
						var change:String = refreshXML.@change;
						var changeConditionItem:ConditionItem = kbsModel.getConditionItemById(change);
						var changeConditionWheres:ArrayCollection = changeConditionItem.getWheres();
						Util.addAll(wheres, changeConditionWheres);
					}
				}				
				
				/*添加自己的值*/
				if(!dimXML.hasOwnProperty('excludes') || (dimXML.hasOwnProperty('excludes') && dimXML.excludes.exclude.(hasOwnProperty('@value') && (@value == getAValue())).length() == 0)){
					var selfWheres:ArrayCollection = getWheres();
					if(selfWheres){
						for each(var selfWhere:WhereVO in selfWheres){
							if(selfWhere.value == preset){
								wheres.addItem(selfWhere);
							}							
						}
					}
				}
				if(!conditionXML.hasOwnProperty('refresh') || (wheres.length > 0 && conditionXML.hasOwnProperty('refresh'))){
					var dimVO:DimVO = new DimVO();
					dimVO.sourceKey = kbsModel.kbsSheetVO.sourceKey;
					dimVO.table = Util.getModeAttribute(dimXML, 'table');
					dimVO.label = dimXML.@label;
					dimVO.value = dimXML.@value;
					dimVO.order = dimXML.@order;
					dimVO.distinct =(dimXML.@distinct=="true");
					dimVO.wheres = wheres;
					dimVO.columns = kbsModel.columns;
					kbsControl.getDimList(dimVO);
				}
				else{
					appendListDimData(null);
				}
			}
		}
		
		private function getListDimResult(event:GetListDimResultEvent):void{
			var dataList:ArrayCollection = event.value;
			appendListDimData(dataList);				
		}
		
		/*添加附加数据*/
		private function appendListDimData(dataList:ArrayCollection):void{
			if(dataList == null){
				dataList = new ArrayCollection();
			}
			updateMessage("ConditionItem.appendListDimData():扩展数据，源数组[" + column +"]长度：" + dataList.length);
			var appendGroupXMLList:XMLList = conditionXML.dim.appendGroup;
			
			for each(var appendGroupXML:XML in appendGroupXMLList){
				var validXML:XMLList = appendGroupXML.child("valid");
				if(kbsModel.isIfsTrue(validXML)){
					var appends:XMLList = appendGroupXML.child("append");
					for each(var appendXML:XML in appends){
						dataList.addItemAt(LabelValueVO.createInstance(appendXML.@label,appendXML.@value),0);
					}
					break;
				}
			}
			for each(var conditionInput:ConditionInputDDList in conditionInputHandler){
				conditionInput.dataProvider = dataList;
				conditionInput.selectedIndex = 0;
				if(preset != null){
					//trace(conditionId + "设置选择项,preset = " + preset);
					for each(var vo:LabelValueVO in dataList){
						if(vo.value == preset){
							conditionInput.selectedItem = vo;
							preset = null;
							break;
						}
					}
				}
			}
			
			//			onChange();
			dispatchUpdateConditionComplete();
		}	
	}
}